import 'dart:io';
import 'package:flutter/material.dart';
import 'calendar_view_modal.dart';
import 'calender_page.dart';

///
/// 日历选择器
///
class CalendarSheet extends Dialog {
  /// 上下文
  final BuildContext buildContext;

  /// 开始时间
  final String? startTime;

  /// 结束时间
  final String? endTime;

  /// 确认回调
  final Function? onTap;

  const CalendarSheet({
    Key? key,
    required this.buildContext,
    this.startTime,
    this.endTime,
    this.onTap,
  }) : super(key: key);

  /// 取消对话框
  static hide(BuildContext context) {
    Navigator.of(context).pop();
  }

  /// 显示对话框
  void show() {
    showDialog(
      context: buildContext,
      barrierDismissible: false,
      useSafeArea: false,
      builder: (BuildContext context) {
        return CalendarContentView(
          callBack: onTap,
          startTime: startTime,
          endTime: endTime,
        );
      },
    );
  }
}

class CalendarContentView extends StatefulWidget {
  final Function? callBack;
  final String? startTime;
  final String? endTime;
  final bool hasInitTime;

  const CalendarContentView({
    Key? key,
    this.callBack,
    this.startTime,
    this.endTime,
    this.hasInitTime = false,
  }) : super(key: key);

  @override
  _CalendarContentViewState createState() => _CalendarContentViewState();
}

class _CalendarContentViewState extends State<CalendarContentView> {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      top: false,
      bottom: false,
      child: Scaffold(
        backgroundColor: const Color(0x60000000),
        resizeToAvoidBottomInset: false,
        body: Container(
          alignment: Alignment.bottomCenter,
          color: Colors.transparent,
          child: Container(
            decoration: const BoxDecoration(
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(4),
                topRight: Radius.circular(4),
              ),
              color: Colors.white,
            ),
            width: double.infinity,
            height: 1298 / 2,
            child: Stack(
              alignment: Alignment.topRight,
              children: [
                Container(
                  padding: EdgeInsets.fromLTRB(
                      0,
                      0,
                      0,
                      Platform.isAndroid
                          ? 16
                          : 16 + MediaQuery.of(context).padding.bottom),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Container(
                        padding: const EdgeInsets.symmetric(
                            horizontal: 16, vertical: 24),
                        decoration: const BoxDecoration(
                          borderRadius: BorderRadius.only(
                              topLeft: Radius.circular(4),
                              topRight: Radius.circular(4)),
                          color: Color.fromRGBO(249, 250, 251, 1),
                        ),
                        child: Row(
                          children: const [
                            Text(
                              '选择时间',
                              style: TextStyle(
                                  fontSize: 18,
                                  color: Color(0xFF1D2023),
                                  fontWeight: FontWeight.w600),
                            )
                          ],
                        ),
                      ),
                      Expanded(
                        child: widget.startTime != null &&
                                widget.endTime != null &&
                                widget.startTime != '' &&
                                widget.endTime != ''
                            ? CalendarPage(
                                startTimeModel: DayModel(
                                    int.parse(
                                        widget.startTime!.split('-').first),
                                    int.parse(widget.startTime!.split('-')[1]),
                                    int.parse(
                                        widget.startTime!.split('-').last),
                                    widget.startTime!.split('-').last,
                                    false,
                                    false),
                                endTimeModel: DayModel(
                                    int.parse(widget.endTime!.split('-').first),
                                    int.parse(widget.endTime!.split('-')[1]),
                                    int.parse(widget.endTime!.split('-').last),
                                    widget.endTime!.split('-').last,
                                    false,
                                    false),
                                hasInitTime: true,
                                selectDateOnTap: (value1, value2) {
                                  Navigator.pop(context);
                                  widget.callBack!(
                                      "${value1.year}-${value1.month > 10 ? value1.month : '0${value1.month}'}-${value1.dayNum > 9 ? value1.dayNum : '0${value1.dayNum}'}~${value2.year}-${value2.month > 10 ? value2.month : '0${value2.month}'}-${value2.dayNum > 9 ? value2.dayNum : '0${value2.dayNum}'}");
                                },
                              )
                            : CalendarPage(
                                selectDateOnTap: (value1, value2) {
                                  Navigator.pop(context);
                                  widget.callBack!(
                                      "${value1.year}-${value1.month > 10 ? value1.month : '0${value1.month}'}-${value1.dayNum > 9 ? value1.dayNum : '0${value1.dayNum}'}~${value2.year}-${value2.month > 10 ? value2.month : '0${value2.month}'}-${value2.dayNum > 9 ? value2.dayNum : '0${value2.dayNum}'}");
                                },
                              ),
                      )
                    ],
                  ),
                ),
                GestureDetector(
                  onTap: () {
                    Navigator.pop(context);
                  },
                  child: Container(
                    margin: const EdgeInsets.only(right: 16, top: 16),
                    color: Colors.white,
                    child: const Icon(Icons.close, size: 24),
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}
